# -*- coding: utf-8 -*-


from PyQt4 import QtCore, QtGui
from color_tools import Ui_MainWindow

class ColorSelect:
    
    def __init__(self):
    
        import sys
        app = QtGui.QApplication(sys.argv)
        MainWindow = QtGui.QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(MainWindow)
        self.initEvent()
        
        MainWindow.show()
        sys.exit(app.exec_())
        
        


    def initEvent(self):
        
        self.ui.rvalue.setValidator(QtGui.QIntValidator(0,255,self.ui.rvalue))
        self.ui.gvalue.setValidator(QtGui.QIntValidator(0,255,self.ui.gvalue))
        self.ui.bvalue.setValidator(QtGui.QIntValidator(0,255,self.ui.bvalue))
        
        
        self.ui.cvalue.setText("#ffffff")
        self.ui.rvalue.setText("255")
        self.ui.gvalue.setText("255")
        self.ui.bvalue.setText("255")
        
       #QtCore.QObject.connect(self.ui.colorPanal,QtCore.SIGNAL("clicked()"),self.setColor)
        self.ui.colorPanal.mousePressEvent  = self.setColor
        QtCore.QObject.connect(self.ui.cvalue,QtCore.SIGNAL("textChanged(QString)"),self.setName)
        QtCore.QObject.connect(self.ui.rvalue,QtCore.SIGNAL("textChanged(QString)"),self.setRgbcolor)
        QtCore.QObject.connect(self.ui.gvalue,QtCore.SIGNAL("textChanged(QString)"),self.setRgbcolor)
        QtCore.QObject.connect(self.ui.bvalue,QtCore.SIGNAL("textChanged(QString)"),self.setRgbcolor)
        

    def setColor(self,event):
        newColor = QtGui.QColorDialog.getColor(self.ui.colorPanal.palette().background().color())
        if newColor.isValid():
            self.ui.rvalue.setText(str(newColor.red()))
            self.ui.gvalue.setText(str(newColor.green()))
            self.ui.bvalue.setText(str(newColor.blue()))
            self.ui.cvalue.setText(str(newColor.name()))
            self.ui.colorPanal.setStyleSheet("background-color: rgb(%d, %d, %d);"%(newColor.red(),newColor.green(),newColor.blue()))


    def setName(self):
        name = self.ui.cvalue.text()
        try:
            newColor = QtGui.QColor(name)
            self.ui.rvalue.setText(str(newColor.red()))
            self.ui.gvalue.setText(str(newColor.green()))
            self.ui.bvalue.setText(str(newColor.blue()))
            #self.ui.cvalue.setText(str(newColor.name()))
            self.ui.colorPanal.setStyleSheet("background-color: rgb(%d, %d, %d);"%(newColor.red(),newColor.green(),newColor.blue()))
        except:
            pass

    
    
    def setRgbcolor(self):
        self.ui.colorPanal.setStyleSheet("background-color: rgb(%s, %s, %s);"%(self.ui.rvalue.text(),self.ui.gvalue.text(),self.ui.bvalue.text()))
        newColor = self.ui.colorPanal.palette().background().color()
        self.ui.cvalue.setText(str(newColor.name()))




if __name__ == "__main__":
    cs = ColorSelect()
    
